package com.siqi.hr.system.common.utils;

import com.siqi.hr.common.utils.Const;
import com.siqi.hr.common.utils.TableConstant;
import com.siqi.hr.system.common.chan.FieldChan;
import com.siqi.hr.system.common.chan.TSQLSelectStat;
import com.siqi.hr.system.common.enums.OperationEnum;
import com.siqi.hr.system.common.model.filter.QueryDataEntity;
import com.siqi.hr.system.common.sql.SField;
import com.siqi.hr.system.common.sql.STable;
import com.siqi.hr.system.custom.entity.EntityOperation;
import com.siqi.hr.system.custom.entity.EntityRecord;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 *  表数据集合
 * @author       xiaogq
 * @version 1.0.0,Aug 21, 2012
 * @since   JDK1.6.0_10 spring3.2.0
 */
public class TableDatas extends ArrayList<Collection<Object>> {

	public TableDatas(TableRow tableRow) {
		super();
		this.tableRow = tableRow;
	}

	private TableRow tableRow;

	public TableRow getTableRow() {
		return tableRow;
	}

	public void setTableRow(TableRow tableRow) {
		this.tableRow = tableRow;
	}

	/**
	 *  根据selectStat查询返回TableDatas的通用方法
	 * @author                            xiaogq
	 * @param jdbcTemplate              传入一个JDBC
	 * @param selectStat                查询对象
	 * @return
	 */
	public static TableDatas queryForList(final JdbcTemplate jdbcTemplate, TSQLSelectStat selectStat) {
		selectStat.init();
		final STable mianTable = selectStat.getTableChan().getMianTable();
		TableRow tableRow = new TableRow(mianTable);
		FieldChan fieldChan = new FieldChan( selectStat.getFieldChan() );
		tableRow.setFieldChan(fieldChan);

		final TableDatas tableDatas = new TableDatas(tableRow);
		final int fieldsize = selectStat.getFieldChan().size();
		String sql = selectStat.toSqlString().toString();
		System.out.println("listSql======"+sql);
		jdbcTemplate.query(sql, new RowCallbackHandler(){
			public void processRow(ResultSet resultset) throws SQLException {
				List<Object> values = new ArrayList<Object>();
				for( int i = 0; i < fieldsize; i++ ){
					values.add( resultset.getObject(i+1) );
				}
				tableDatas.add(values);
			}
		});
		return tableDatas;
	}

	/**
	 * 通用查询客户化实体详情页面数据
	 * @author                            xiaogq
	 * @param jdbcTemplate              传入一个JDBC
	 * @param selectStat                查询对象
	 * @return
	 */
	public static Map<String, Object> queryViewDatasById(final JdbcTemplate jdbcTemplate, TSQLSelectStat selectStat) {
		selectStat.init();
		final STable mianTable = selectStat.getTableChan().getMianTable();
		TableRow tableRow = new TableRow(mianTable);
		FieldChan fieldChan = new FieldChan( selectStat.getFieldChan() );
		tableRow.setFieldChan(fieldChan);
		final TableDatas tableDatas = new TableDatas(tableRow);
		final int fieldsize = selectStat.getFieldChan().size();
		String sql = selectStat.toSqlString().toString();
		System.out.println("viewSql======"+sql);
		Map<String,Object> dataMaps = jdbcTemplate.queryForMap(sql);
		return dataMaps;
	}

	/**
	 *  处理列表数据特殊字段值的数据
	 *  @author            xiaogq
	 *  @param queryDataEntity     列表查询条件、分页对象信息
	 * @return                       List<Map<String, Object>>
	 */
	public List<Map<String, Object>> extractValus(QueryDataEntity queryDataEntity){
		List<Map<String, Object>> r = new ArrayList<Map<String,Object>>();
		if(size() > 0){
			for( Collection<Object> values : this ){
				Map<String, Object> map = new HashMap<String, Object>();
				Iterator<Object> valueIterable = values.iterator();
				Iterator<SField> fIterator = tableRow.getFieldChan().iterator();
				while( valueIterable.hasNext() ){
					SField field = fIterator.next();
					String fieldByName = field.getByName();
					Object fieldVal = valueIterable.next();
					if(StringUtils.isBlank(fieldByName)){
						fieldByName = field.getName();
					}
					if(fieldByName.equals("name_varchar")){
						String name = "";
						if(queryDataEntity.getTable()){//如果是弹窗列表数据
							name = "<span style='color:#4a90e2;cursor:pointer;'>"+fieldVal+"</span>";
						}else{//如果是列表数据
							name = "<span onclick='fun_view(this)' style='color:#4a90e2;cursor:pointer;' id='" + map.get("id") +"' tableCode='"+queryDataEntity.getTableCode()+ "' name='" + fieldVal + "' " + " title='查看详情'>"+fieldVal+"</span>";
						}
						map.put(fieldByName, name);
						map.put("name",fieldVal);
					}else if(fieldByName.endsWith("_precent") && null != fieldVal){//出来百分比
						map.put(fieldByName, Float.parseFloat( fieldVal.toString() ) * 100 +"%");
					}else if(field.isQuote()){
						map.put(StringUtils.substringBeforeLast(fieldByName, Const._VAL)+"_id", fieldVal);
					} else if(StringUtils.endsWith(fieldByName, Const._VAL)){	//把引类型的字段id值替换成名称
						map.put(StringUtils.substringBeforeLast(fieldByName, Const._VAL), fieldVal);
					}else if(fieldByName.endsWith("_img")){//出来图片类型字段
						if(queryDataEntity.getTable()) {//如果是弹窗列表数据
							map.put(fieldByName, "<a href='javascript:void(0);' style='color:#4a90e2;cursor:pointer;text-decoration:none;'>查看图片</a>");
						}else{//如果是列表数据
							map.put(fieldByName, "<a href="+fieldVal+" style='color:#4a90e2;cursor:pointer;text-decoration:none;' target='_blank'>查看图片</a>");
						}
					} else {
						map.put(fieldByName, fieldVal);
					}
				}
				r.add(map);
			}
		}
		return r;
	}

}
